package com.atguigu.crowd.mvc.controller;


import com.atguigu.crowd.constant.CrowdConstant;
import com.atguigu.crowd.service.api.ProjectService;
import com.atguigu.crowd.until.ResultEntity;
import com.github.pagehelper.PageInfo;
import crowd.entity.Project;
import crowd.entity.ProjectVO;
import crowd.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

@Controller
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @RequestMapping("/projectAdmin/to/page.html")
    public String getPageInfo(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                    @RequestParam(value = "keyword", defaultValue = "") String keyword,
                                                                ModelMap modelMap){

        PageInfo<ProjectVO> pageInfo = projectService.getPageInfo(keyword, pageNum, pageSize);

        modelMap.addAttribute(CrowdConstant.ATTR_NAME_PAGE_BY_PROJECT, pageInfo);
        return "project-admin";
    }

    @ResponseBody
    @RequestMapping("/project/audit.json")
    @PreAuthorize("hasAnyRole('管理员', '项目审核员')")
    public ResultEntity<String> auditProject(Integer id){

        projectService.auditProject(id);
        return ResultEntity.successWithoutData();
    }
}
